﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EEG_NIRS_Record
{
    public partial class EEGForm : Form
    {
        MainForm _mainform;

        Graphics g;
        Bitmap image;
        Pen black_pen;
        Pen blue_pen;
        SolidBrush white_brush;
        Font font;

        public Rectangle rc;
        int x;
        int num_of_eeg;
        double[] old;
        int eeg_zoom;
        
        public EEGForm(MainForm mainform)
        {            
            InitializeComponent();

            _mainform = mainform;

            image = new Bitmap(pb_eeg.Width, pb_eeg.Height); 
            pb_eeg.Image = image;
            g = Graphics.FromImage(image);


            black_pen = new Pen(Color.Black, 1);
            blue_pen = new Pen(Color.Blue, 1);
            white_brush = new SolidBrush(Color.White);
            font = new Font("宋体", 9, FontStyle.Bold);

            rc = new Rectangle();
            x = 0;
            num_of_eeg = _mainform.configuration.label_eeg_list.Count;
            old = new double[num_of_eeg];           


            trackBar.TrackBar.Maximum = 999;
            trackBar.TrackBar.Minimum = 0;
            trackBar.TrackBar.Value = _mainform.trackBar.TrackBar.Value;
            eeg_zoom = trackBar.TrackBar.Value;
        }

        private void EEGForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            _mainform.trackBar.TrackBar.Value = trackBar.TrackBar.Value;
            _mainform.where_eeg = 0;
            _mainform.RecoverTabPage(0);
        }
        
        private void pb_eeg_Resize(object sender, EventArgs e)
        {
            image = new Bitmap(pb_eeg.Width, pb_eeg.Height);
            pb_eeg.Image = image;
            g = Graphics.FromImage(image);
            //lock (g)
            //{
            //    image = new Bitmap(pb_eeg.Width, pb_eeg.Height);
            //    pb_eeg.Image = image;
            //    g = Graphics.FromImage(image);
            //}
            //g = Graphics.FromImage(image);
        }

        public void DrawEEG(int[,] data, int len)
        {
            if (x >= pb_eeg.Width - 1)
            {
                x = 0;
            }

            g.FillRectangle(white_brush, x + 1, 0, len + 1, pb_eeg.Height);
            rc.X = x - 10;
            for (int i = 0; i < len; i++)
            {
                lock (_mainform.index_of_show)
                {
                    int num_of_show = _mainform.index_of_show.Count;
                    for (int j = 0; j < num_of_show; j++)
                    {
                        g.DrawLine(blue_pen, x, (int)(pb_eeg.Height * (j + 1) / (num_of_show + 1) - (int)old[_mainform.index_of_show[j]] / eeg_zoom), x + 1, (int)(pb_eeg.Height * (j + 1) / (num_of_show + 1) - data[i, _mainform.index_of_show[j]] / eeg_zoom));
                    }
                }
                for (int j = 0; j < num_of_eeg; j++)
                {
                    old[j] = data[i, j];
                }
                x++;
            }
            // 赋值画topo的数据
            lock (_mainform.eeg_data_for_topo)
            {
                _mainform.eeg_data_for_topo = old.ToArray();
            }            
            //for (int i = 0; i < num_of_eeg; i++)
            //{
            //    eeg_data_for_topo[i] = old[i];
            //}
            g.DrawLine(black_pen, x + 1, 0, x + 1, pb_eeg.Height);
            rc.Y = 0;
            rc.Width = len + 2 + 30;
            rc.Height = pb_eeg.Height;
            pb_eeg.Invalidate(rc);
        }
        private void trackBar_ValueChanged(object sender, EventArgs e)
        {
            eeg_zoom = 100 - trackBar.TrackBar.Value;
        }

        private void btn_ch_Click(object sender, EventArgs e)
        {
            if (_mainform.channelform.ShowDialog() == DialogResult.OK)
            {
                panel_ch.Invalidate();
            }
        }

        private void btn_filter_Click(object sender, EventArgs e)
        {

        }

        private void panel_ch_Paint(object sender, PaintEventArgs e)
        {
            Graphics gg = e.Graphics;
            gg.FillRectangle(white_brush, 0, 0, panel_ch.Width, panel_ch.Height);
            for (int i = 0; i < _mainform.index_of_show.Count; i++)
            {
                gg.DrawString(_mainform.configuration.label_eeg_list[_mainform.index_of_show[i]], font, Brushes.Black, 50, panel_ch.Height * (i + 1) / (_mainform.index_of_show.Count + 1) - 7);
            }
        }

        private void panel_ch_Resize(object sender, EventArgs e)
        {
            panel_ch.Invalidate();
        }
    }
}
